package com.play.parse.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.play.parse.JsoupHandler;

import edu.uci.ics.crawler4j.parser.ExtractedUrlAnchorPair;

public class ShangpinHandler extends JsoupHandler{
	protected static final Logger logger = Logger.getLogger(ShangpinHandler.class.getName()); 
	
	
	/**
	 * 商品网
	 */
	protected void productSeed(Document doc) {
		Elements type = doc.select("#spIndex_window_typeList");
		if(type.size()>0){
			Elements sees = type.select("dt>a");
			for (Element element : sees) {
				ExtractedUrlAnchorPair curUrl = new ExtractedUrlAnchorPair();
				String seedurl = element.attr("href");
				curUrl.setHref(seedurl);
				outgoingurls.add(curUrl);
			}
		}
	}

	protected void productList(Document doc) {
		Elements e = doc.select(".spProduct_listbox").select(".product_infomation").select("p");
		if(e.size()>0){
			List<String> prodects = new ArrayList<String>();
			int x = 1;
			//获取产品名称和价格
			for (Element p : e) {
				if(x%2==1){
					prodects.add("<product>");
				}
				Elements name = p.select("a");
				if(name.size()>0){
					String attr = name.attr("href");
					prodects.add(name.text());
					prodects.add(attr);
					ExtractedUrlAnchorPair curUrl = new ExtractedUrlAnchorPair();
					if(StringUtils.isBlank(attr)){
						continue;
					}
					curUrl.setHref(attr);
					outgoingurls.add(curUrl);
				}else{
					prodects.add(p.attr("comprice"));
				}
				if(x%2==0){
					prodects.add("</product>");
				}
				x++;
			}
			
			try {
				FileUtils.writeLines(new File(filePath+File.separator+getClass().getName()+"list.txt"), prodects,true);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			//获取分页信息
			Elements pages = doc.select(".spList-page a");
			for (Element page : pages) {
				String pageUrl = page.attr("href");
				//logger.info(pageUrl);
				ExtractedUrlAnchorPair curUrl = new ExtractedUrlAnchorPair();
				curUrl.setHref(pageUrl);
				outgoingurls.add(curUrl);
			}
		}
	}

	protected void productInfo(String contextURL, Document doc) {
		Elements info = doc.select("div[class=spNewDetail_border box]");
		if(info.size()>0){
			
			List<String> prodect = new ArrayList<String>();
			prodect.add("<product>");
			prodect.add(contextURL);
			//获取位置（类别路径）信息
			Elements type = doc.select(".sp_crumb");
			prodect.add(type.text());
			
			//产品名称
			Elements name = doc.select("#spDetail_proInfo").select("h2,h1");
			prodect.add(name.text());
			//获取产品信息
			Elements atrr = doc.select("#spDeatil_procs");
			prodect.add(atrr.text());
			
			//TODO 获取价格信息 拿不到
			//logger.info(info.select(".p-price").text());
			prodect.add("</product>");
			try {
				FileUtils.writeLines(new File(filePath+File.separator+getClass().getName()+".txt"), prodect,true);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	@Override
	protected String getFilePath() {
		return  "F:\\machine_study\\中文语料\\电商分类\\mybest";
	}
	
}
